package cn.itrip.biz.controller;


import cn.itrip.beans.dtos.Dto;
import cn.itrip.beans.dtos.ErrorCode;
import cn.itrip.beans.dtos.InputDto;
import cn.itrip.beans.pojo.ItripHotelOrder;
import cn.itrip.beans.pojo.ItripHotelRoom;
import cn.itrip.beans.pojo.ItripUser;
import cn.itrip.beans.vo.*;
import cn.itrip.beans.vo.tokentakevo.OrderAndLinkUserVo;
import cn.itrip.biz.service.IItripHotelOrderService;
import cn.itrip.common.Page;
import cn.itrip.common.ValidationToken;
import cn.itrip.dao.ItripHotelOrderMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;


/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author 滕召伟
 * @since 2020-02-27
 */
@CrossOrigin
@Api(tags = "订单查询")
@RestController
@RequestMapping("/biz/api/hotelorder")
public class ItripHotelOrderController {
    @Resource
    private IItripHotelOrderService iItripHotelOrderService;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private ItripHotelOrderMapper itripHotelOrderMapper;

    /**
     * 查询所有订单
     *
     * @param pa
     * @return
     */

    @ApiOperation(value = "查询全部订单", httpMethod = "Post", protocols = "HTTP", produces = "application/json;charset=utf-8", response = Dto.class, notes = "全部订单")
    @PostMapping(value = "/getpersonalorderlist", produces = "application/json;charset=utf-8")
    public Dto listPersonalOrder(@ApiParam(name = "pa", value = "分页对象,查询对象", required = true) @RequestBody PageList pa) {
        ItripHotelOrder order = new ItripHotelOrder();
        if (pa.getOrderNo() == "") {
            pa.setOrderNo(null);
        }
        order.setOrderNo(pa.getOrderNo());
        if (pa.getLinkUserName() == "") {
            pa.setLinkUserName(null);
        }
        order.setLinkUserName(pa.getLinkUserName());
        if (pa.getStartDate() == "") {
            pa.setStartDate(null);
        }
        order.setCreationDate(pa.getStartDate());
        if (pa.getEndDate() == "") {
            pa.setEndDate(null);
        }
        order.setCheckOutDate(pa.getEndDate());
        Integer orderType = Integer.valueOf(pa.getOrderType());
        if (orderType == -1) {
            orderType = null;
        }
        order.setOrderType(orderType);
        if (pa.getOrderStatus() == -1) {
            pa.setOrderStatus(null);
        }
        order.setOrderStatus(pa.getOrderStatus());
        //分页查询
        Page<ItripHotelOrder> page = iItripHotelOrderService.listHotelOrder(pa.getPageNo(), pa.getPageSize(), order);
        Map<String, Object> map = new HashMap<>();
        map.put("rows", page.getRows());
        map.put("curPage", page.getPageNo());
        map.put("pageCount", page.getPageCount());
        map.put("pageSize", page.getPageSize());
        map.put("total", page.getTotalRow());
        map.put("beginPos", page.getBeginPos());
        if (order.getOrderNo() != null || order.getLinkUserName() != null || order.getCreationDate() != null || order.getCheckOutDate() != null) {
            return InputDto.data("订单详细查询", map);
        } else {
            return InputDto.data(map);
        }

    }

    /**
     * 根据订单id查询订单详情
     *
     * @param orderId
     * @return
     */
    @ApiOperation(value = "订单详情", httpMethod = "Get", protocols = "HTTP", produces = "application/json;charset=utf-8", response = Dto.class, notes = "订单详情")
    @GetMapping(value = "/getpersonalorderinfo/{orderId}", produces = "application/json;charset=utf-8")
    public Dto getParticulars(@ApiParam(name = "orderId", value = "订单ID", required = true) @PathVariable Long orderId) {
        OrderRoomVo orderRoomVo = iItripHotelOrderService.getOrderId(orderId);
        if (orderRoomVo != null) {
            orderRoomVo.setRoomPayType(orderRoomVo.getPay());
            return InputDto.data("获取订单详情成功", orderRoomVo);
        }
        return InputDto.error(ErrorCode.QUERY_ERROR);

    }

    /**
     * 查询入住信息
     *
     * @param orderId
     * @return
     */
    @ApiOperation(value = "入住信息", httpMethod = "Get", protocols = "HTTP", produces = "application/json;charset=utf-8", response = Dto.class, notes = "入住信息")
    @GetMapping(value = "/getpersonalorderroominfo/{orderId}", produces = "application/json;charset=utf-8")
    public Dto getRegister(@ApiParam(name = "orderId", value = "订单ID", required = true) @PathVariable("orderId") Long orderId) {
        OrderRoomHotelVO order = iItripHotelOrderService.getOrderRommHtoId(orderId);
        if (order != null) {
            //时间转换，讲前台传入的时间转换成yyyy-MM-dd 并赋值给对象
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Object inDate = simpleDateFormat.format(order.getCheckInDate());
            Object outDate = simpleDateFormat.format(order.getCheckOutDate());
            order.setRoomBedTypeName(order.getName());
            order.setCheckInDate(inDate);
            order.setCheckInWeek(inDate);
            order.setCheckOutDate(outDate);
            order.setCheckOutWeek(outDate);
            return InputDto.data(order);
        }
        return InputDto.error(ErrorCode.QUERY_ERROR);

    }

    /**
     * 查询房型ID
     *
     * @param hotelId
     * @param orderId
     * @return
     */
    @ApiOperation(value = "房型Id", httpMethod = "Get", protocols = "HTTP", produces = "application/json;charset=utf-8", response = InputDto.class, notes = "房型Id")
    @GetMapping(value = "/getpersonalorderoom/{hotelId}/{orderId}", produces = "application/json;charset=utf-8")
    public Dto getPersonalOrderoom(@ApiParam(name = "hotelId", value = "酒店id", required = true) @PathVariable("hotelId") Long hotelId,
                                   @ApiParam(name = "orderId", value = "订单id", required = true) @PathVariable("orderId") Long orderId) {
        ItripHotelRoom room = iItripHotelOrderService.getHotelId(hotelId, orderId);
        if (room != null) {
            Map<String, Object> map = new HashMap<>();
            map.put("roomId", room.getId());
            return InputDto.data(map);
        }
        return InputDto.error(ErrorCode.QUERY_ERROR);
    }

    /**
     * 获取订单详细信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "获取订单信息", httpMethod = "Get", protocols = "HTTP", produces = "application/json;charset=utf-8", response = InputDto.class, notes = "订单信息")
    @GetMapping(value = "/queryOrderById/{id}", produces = "application/json;charset=utf-8")
    public Dto getHotelOrder(@ApiParam(name = "id", value = "订单Id", required = true) @PathVariable Long id) {

        OrderLinkUserVo userVo = iItripHotelOrderService.getOrderLinkUserVoId(id);
        if (userVo != null) {
            return InputDto.data(userVo);
        }
        return InputDto.error(ErrorCode.QUERY_ERROR);
    }

    /**
     * 根据返回的数据查询预订房间信息
     *
     * @param itripHotelOrder
     * @return
     */
    @ApiOperation(value = "查询房间信息", httpMethod = "Post", protocols = "HTTP", produces = "application/json;charset=utf-8", response = InputDto.class, notes = "查询房间信息")
    @PostMapping(value = "/getpreorderinfo", produces = "application/json;charset=utf-8")
    public Dto getPreOrderinfo(@ApiParam(name = "itripHotelOrder", value = "订单对象", required = true) @RequestBody ItripHotelOrder itripHotelOrder) {

        StoreOrderRommHotelVo storeOrderRommHotelVo = iItripHotelOrderService.getHotelProductStoreDic(itripHotelOrder);
        if (storeOrderRommHotelVo != null) {
            return InputDto.data(storeOrderRommHotelVo);
        }
        return InputDto.error(ErrorCode.QUERY_ERROR);
    }


    /**
     * 预订和修改订单
     *
     * @param orderAndLinkUserVo
     * @param request
     * @return
     */
    @ApiOperation(value = "新增和修改订单", httpMethod = "Post", protocols = "HTTP", produces = "application/json;charset=utf-8", response = InputDto.class, notes = "新增和修改订单")
    @PostMapping(value = "/addhotelorder", produces = "application/json;charset=utf-8")
    public Dto addAndUpdateOrder(@ApiParam(name = "orderAndLinkUserVo", value = "订单和用户对象", required = true) @RequestBody OrderAndLinkUserVo orderAndLinkUserVo,
                                 HttpServletRequest request) {
        String token = request.getHeader("token");
        ValidationToken validationToken = new ValidationToken();
        ItripUser user = validationToken.getToken(token, redisTemplate);
        // if判断如果有订单号和id那么就进入修改，如果传入的值没有id和订单号就进入新增,
        if (orderAndLinkUserVo.getId() != null && orderAndLinkUserVo.getOrderNo() != null) {
            if (user != null && !"".equals(user)) {
                //获取redis里面的用户id
                orderAndLinkUserVo.setModifiedBy(user.getId());
                if (orderAndLinkUserVo.getCount() != null) {
                    for (int i = 0; i < orderAndLinkUserVo.getLinkUser().size(); i++) {
                        if (i == (orderAndLinkUserVo.getCount() * 3)) {
                            return InputDto.error(ErrorCode.IS_GREATER_THAN_THE_ERROR);
                        }
                    }
                }
                //预订完成查询酒店
                ItripHotelOrder itripHotelOrder = itripHotelOrderMapper.getItripHotelOrder(orderAndLinkUserVo.getOrderNo());
                //修改订单
                iItripHotelOrderService.updateHotelOrder(orderAndLinkUserVo);
                return InputDto.data("订单修改成功!", itripHotelOrder);
            }

        } else {
            //如果没有订单号和订单id那么证明没有该订单，进入新增订单
            if (user != null && !"".equals(user)) {
                //酒店预订
                orderAndLinkUserVo.setCreatedBy(user.getId());
                if (orderAndLinkUserVo.getCount() != null) {
                    for (int i = 0; i < orderAndLinkUserVo.getLinkUser().size(); i++) {
                        if (i == (orderAndLinkUserVo.getCount() * 3)) {
                            return InputDto.error(ErrorCode.IS_GREATER_THAN_THE_ERROR);
                        }
                    }
                }
                iItripHotelOrderService.addHotelOrder(orderAndLinkUserVo);
                //预订完成查询酒店
                ItripHotelOrder itripHotelOrder = itripHotelOrderMapper.getItripHotelOrder(orderAndLinkUserVo.getOrderNo());

                //进行if判断
                if (itripHotelOrder.getId() != 0 && !"".equals(itripHotelOrder)) {
                    return InputDto.data("预订成功!", itripHotelOrder);
                }
                return InputDto.error(ErrorCode.ADD_THE_ERROR);
            }
        }
        return InputDto.error(ErrorCode.CONTENT_ACQUISITION_ERROR);
    }


}

